package org.dromara.stock.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.stock.domain.vo.StockInfoVo;
import org.dromara.stock.domain.bo.StockInfoBo;
import org.dromara.stock.service.IStockInfoService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 股票详情
 * 前端访问路由地址为:/stock/stockInfo
 *
 * @author dongfs
 * @date 2024-06-30
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/stockInfo")
public class StockInfoController extends BaseController {

    private final IStockInfoService stockInfoService;

    /**
     * 查询股票详情列表
     */
    @SaCheckPermission("stock:stockInfo:list")
    @GetMapping("/list")
    public TableDataInfo<StockInfoVo> list(StockInfoBo bo, PageQuery pageQuery) {
        return stockInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出股票详情列表
     */
    @SaCheckPermission("stock:stockInfo:export")
    @Log(title = "股票详情", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(StockInfoBo bo, HttpServletResponse response) {
        List<StockInfoVo> list = stockInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "股票详情", StockInfoVo.class, response);
    }

    /**
     * 获取股票详情详细信息
     *
     * @param code 主键
     */
    @SaCheckPermission("stock:stockInfo:query")
    @GetMapping("/{code}")
    public R<StockInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String code) {
        return R.ok(stockInfoService.queryById(code));
    }

    /**
     * 新增股票详情
     */
    @SaCheckPermission("stock:stockInfo:add")
    @Log(title = "股票详情", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody StockInfoBo bo) {
        return toAjax(stockInfoService.insertByBo(bo));
    }

    /**
     * 修改股票详情
     */
    @SaCheckPermission("stock:stockInfo:edit")
    @Log(title = "股票详情", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody StockInfoBo bo) {
        return toAjax(stockInfoService.updateByBo(bo));
    }

    /**
     * 删除股票详情
     *
     * @param codes 主键串
     */
    @SaCheckPermission("stock:stockInfo:remove")
    @Log(title = "股票详情", businessType = BusinessType.DELETE)
    @DeleteMapping("/{codes}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] codes) {
        return toAjax(stockInfoService.deleteWithValidByIds(List.of(codes), true));
    }
}
